package pers.rcklos.myserver.controller;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import pers.rcklos.myserver.biz.AdminBiz;
import pers.rcklos.myserver.biz.UserBiz;
import pers.rcklos.myserver.entity.SpAdmin;
import pers.rcklos.myserver.entity.SpUser;

@Controller
public class UserController {
	
	private UserBiz userBiz;
	private AdminBiz adminBiz;
	
	@RequestMapping("/")
	public String index() {
		return "redirect: ./user/login";
	}
	
	@GetMapping(value = { "/user/login"})
	public String login(String code, String pwd, HttpSession session) throws IOException {
		if((code == null || pwd == null) || (code == "" || pwd == "")) {
			return "login";
		}
		SpUser user = userBiz.userLogin(code, pwd);								// 获取用户对象
		if(user != null) { // 登录成功
			System.out.println("login successfully");
			session.setAttribute("user", user);
			SpAdmin admin = adminBiz.verifyAdmin(user);
			if( admin != null) {												// 验证是否管理员
				session.setAttribute("admin", admin);
				return "redirect:query_all";
			}
			else {
				session.setAttribute("admin", admin);
				return "redirect:../paper/query";
			}
		}
		else {
			return "login";
		}
	}
	
	@GetMapping("/user/delete")
	@ResponseBody
	public String queryDeleteUser(int uid) {
		JSONObject object = new JSONObject();
		boolean success = userBiz.deleteUser(uid);
		object.put("state", success ? "ok" : "err");
		return object.toJSONString();
	}
	
	@GetMapping("/user/query_all")
	public String queryAllUsers(@RequestParam(defaultValue = "0")int start, Model model, HttpSession session) throws ServletException, IOException {
		System.out.println("queryAllUsers: 1");
		if(session.getAttribute("admin") == null) {
			return "login";
		}
		System.out.println("queryAllUsers: 2");
//		ArrayList<SpUser> list = userBiz.queryAllUsers();
		model.addAttribute("page", start);
		ArrayList<SpUser> list = userBiz.queryPageOfUsers(start, 10);
		System.out.println("queryAllUsers: 3");
		model.addAttribute("users", list);
//		if(list == null) req.setAttribute("test", "list is null");
		if(list == null) model.addAttribute("test", "list is null");
		else model.addAttribute("query_size", list.size());
		
		// 装配head部分
		model.addAttribute("thead", 0);			// 用户表
		System.out.println("UserServlet: admin.jsp");
		return "admin";
	}
	
	@GetMapping("/user/register")
	public String userRegister(String name, String pwd, String phone) throws IOException {
		if(((pwd==null||name==null)||phone==null)||(("".equals(pwd)||"".equals(name))||"".equals(phone))) {
			return "register";
		}
		
		if(userBiz.userRegister(name, pwd, phone)) {
			return "redirect:login";
		}
		
		return "register";
	}
	
	@GetMapping(value="/user/modifyUserInfo")
	@ResponseBody
	public String modifyUserInfo(String name, String nick, String phone, HttpSession session) throws IOException {
		SpUser user = (SpUser) session.getAttribute("admin-md-user");
		if(user==null) {
			return "redirect:query_all";
		}
		
		SpUser newUserInfo = new SpUser();
		newUserInfo.setUserName(name!=null?name:user.getUserName());
		newUserInfo.setUserNickName(nick!=null?nick:user.getUserNickName());
		newUserInfo.setUserPhone(phone!=null?phone:user.getUserPhone());
		
		boolean isSuccess = userBiz.modifyUserInfo(user, newUserInfo);
		return isSuccess?"修改成功!":"修改失败!";
	}
	
	@GetMapping(value="/user/query", produces="application/json")
	@ResponseBody
	public String queryUserInfo(HttpSession session) {
		SpUser qUser = (SpUser) session.getAttribute("admin-md-user");
		JSONObject json = new JSONObject();
		if(qUser==null) {
			json.put("status", "err");
		}
		else {
			json.put("status", "ok");
			json.put("name", qUser.getUserName());
			json.put("nick", qUser.getUserNickName());
			json.put("phone", qUser.getUserPhone());
			json.put("create", qUser.getCreateTime().toString());
			json.put("last", qUser.getLastLoginTime().toString());
		}
		return json.toJSONString();
	}
	
	@GetMapping("/user/modify")
	public String modifyUserInfo(Integer uid, HttpSession session) {
		if(uid == null) {
			return "redirect:query_all";
		}
		
		SpUser user = userBiz.queryUserByUid(uid);
		if(user == null) {
			return "redirect:login";
		}
		
		System.out.println("UserServlet: save-user");
		session.setAttribute("admin-md-user", user);
		return "modify";
	}
	
	@GetMapping("/user/findPassword")
	public String modifyPwd(String name, String pwd, String phone) throws IOException {
		if(((pwd==null||name==null)||phone==null)||(("".equals(pwd)||"".equals(name))||"".equals(phone))) {
			return "findPassword";
		}
		
		if(userBiz.userModifyPassword(name, pwd, phone)) {
			System.out.println("用户[" + name + "] 修改密码成功");
			return "login";
		}
		
		return "findPassword";
	}
	
	@Autowired
	public void setUserBiz(UserBiz userBiz) {
		this.userBiz = userBiz;
	}
	
	@Autowired
	public void setAdminBiz(AdminBiz adminBiz) {
		this.adminBiz = adminBiz;
	}
}
